package com.chang.multithread.CorrectPostureStep;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;


@Slf4j
public class Wita2 {
    static final Object room =new Object();
    static boolean hasTokeout=false; //有没有外卖
    static boolean hasAfflatus=false;//有没有灵感

    public static void main(String[] args) {
        new Thread(()->{
            synchronized (room){
                log.debug("有没有灵感？[{}]",hasAfflatus);
                if (!hasAfflatus){
                //while (!hasAfflatus){   //判断是不是灵感来了
                    log.debug("没有灵感，休息一下");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("有灵感么[{}]",hasAfflatus);
                if (hasAfflatus){
                    log.debug("灵感来了,准备开工！");
                }else {
                    log.debug("没有灵感，无法干活");
                }
            }
        },"t1").start();

        new Thread(()->{
            synchronized (room){
                log.debug("有没有外卖？[{}]",hasTokeout);
                if (!hasAfflatus){
                    log.debug("好饿，点个外卖吃吃");
                    try {
                        room.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("外卖到了么[{}]",hasTokeout);
                if (hasTokeout){
                    log.debug("外卖来了,准备开工！");
                }else {
                    log.debug("好饿，干不了活");
                }
            }
        },"t2").start();


       try {
           TimeUnit.SECONDS.sleep(1);
         } catch (InterruptedException e) {
         e.printStackTrace(); }

       new Thread(()->{
           synchronized (room) {
               hasTokeout = true;
               log.debug("外卖到了！！！");
              // room.notify();
               room.notifyAll();
           }
       },"送外卖的").start();

    }
}
